package com.leetcode.others;

/**
 * @author Dennis Li
 * @date 2020/7/17 18:52
 */
public class PlusOne_66 {
    public int[] plusOne(int[] digits) {
        int carry = 1;
        for (int i = digits.length - 1; i >= 0; i--) {
            if (digits[i] == 9 && carry == 1) {
                // 如果已经有进位，那么进位的值不变
                digits[i] = 0;
            } else if (carry == 1) {
                digits[i] += carry;
                carry = 0;
            }
        }
        // 如果超界
        if (digits[0] == 0 && carry == 1) {
            int[] ans = new int[digits.length + 1];
            ans[0] = 1;
            for (int i = 0; i < digits.length; i++) {
                ans[i + 1] = digits[i];
            }
            return ans;
        }
        return digits;
    }
}
